package com.ble.fragment;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ble.BleDetailActivity;
import com.ble.R;
import com.ble.base.BaseFragment;
import com.ble.utlis.TextTools;
import com.ble.utlis.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import androidx.appcompat.app.AlertDialog;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentActivity;

public class StatusFragment extends BaseFragment {


    SimpleDateFormat sff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private BleDetailActivity activity;

    @Override
    protected int getContentResid() {
        return R.layout.fragment_status;
    }

    Map<Integer, TextView> map = new HashMap<>();


    public void addMsg(final String msg) {
        try {
            self.runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                    String msg1 = (sff.format(new Date()) + "  " + msg);
//
//                    String text = tv_msg.getText().toString();
//                    String con = msg1 + "\r\n" + text;
//                    if (con.length() > 2000) {
//                        con = msg1;
//                    }
//                    tv_msg.setText(con);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    @Override
    protected void init(View view) {
        activity = (BleDetailActivity) getActivity();
        TextView tv_00001 = view.findViewById(R.id.tv_00001);
        TextView tv_00002 = view.findViewById(R.id.tv_00002);
        TextView tv_00010 = view.findViewById(R.id.tv_00010);

        TextView tv_40001 = view.findViewById(R.id.tv_40001);
        TextView tv_40002 = view.findViewById(R.id.tv_40002);
        TextView tv_40003 = view.findViewById(R.id.tv_40003);
        TextView tv_40004 = view.findViewById(R.id.tv_40004);
        TextView tv_40005 = view.findViewById(R.id.tv_40005);
        TextView tv_40006 = view.findViewById(R.id.tv_40006);
        TextView tv_40007 = view.findViewById(R.id.tv_40007);
        TextView tv_40008 = view.findViewById(R.id.tv_40008);
        TextView tv_40009 = view.findViewById(R.id.tv_40009);
        TextView tv_40010 = view.findViewById(R.id.tv_40010);
        TextView tv_40011 = view.findViewById(R.id.tv_40011);
        TextView tv_40012 = view.findViewById(R.id.tv_40012);
        TextView tv_40013 = view.findViewById(R.id.tv_40013);
        TextView tv_40014 = view.findViewById(R.id.tv_40014);
        TextView tv_40015 = view.findViewById(R.id.tv_40015);
        TextView tv_40016 = view.findViewById(R.id.tv_40016);
        TextView tv_40017 = view.findViewById(R.id.tv_40017);
        TextView tv_40018 = view.findViewById(R.id.tv_40018);
        TextView tv_40019 = view.findViewById(R.id.tv_40019);
        TextView tv_40020 = view.findViewById(R.id.tv_40020);
        TextView tv_40021 = view.findViewById(R.id.tv_40021);
        TextView tv_40022 = view.findViewById(R.id.tv_40022);
        TextView tv_40023 = view.findViewById(R.id.tv_40023);
        TextView tv_40024 = view.findViewById(R.id.tv_40024);
        TextView tv_40027 = view.findViewById(R.id.tv_40027);
        TextView tv_40029 = view.findViewById(R.id.tv_40029);
        TextView tv_4002526 = view.findViewById(R.id.tv_4002526);


        map.put(00001, tv_00001);
        map.put(00002, tv_00002);
        map.put(00010, tv_00010);
        map.put(00010, tv_00010);


        map.put(40001, tv_40001);
        map.put(40002, tv_40002);
        map.put(40003, tv_40003);
        map.put(40004, tv_40004);
        map.put(40005, tv_40005);
        map.put(40006, tv_40006);
        map.put(40007, tv_40007);
        map.put(40008, tv_40008);
        map.put(40009, tv_40009);
        map.put(40010, tv_40010);

        map.put(40011, tv_40011);
        map.put(40012, tv_40012);
        map.put(40013, tv_40013);
        map.put(40014, tv_40014);
        map.put(40015, tv_40015);
        map.put(40016, tv_40016);
        map.put(40017, tv_40017);
        map.put(40018, tv_40018);
        map.put(40019, tv_40019);
        map.put(40020, tv_40020);
        map.put(40021, tv_40021);
        map.put(40022, tv_40022);
        map.put(40023, tv_40023);
        map.put(40024, tv_40024);
        map.put(40027, tv_40027);
        map.put(4002526, tv_4002526);
        map.put(40029, tv_40029);



        CardView cd_senior = view.findViewById(R.id.cd_senior);
        cd_senior.setVisibility(View.GONE);
        TextView tv_senior = view.findViewById(R.id.tv_senior);
        tv_senior.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_senior.getPaint().setAntiAlias(true);//抗锯齿
        tv_senior.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_alert, null);
                final EditText editText = inflate.findViewById(R.id.et_adress);
                editText.setMaxLines(8);
                builder.setView(inflate).setTitle("请输入查看高级参数密码").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String s = editText.getText().toString();
                        if (s.isEmpty()) {
                            Toast.makeText(getActivity(), "密码不能为空", Toast.LENGTH_SHORT).show();

                            return;
                        }

                        if (s.equals(activity.getUserpw())) {
                            tv_senior.setVisibility(View.GONE);
                            cd_senior.setVisibility(View.VISIBLE);
                        } else {
                            ToastUtil.showShort(getActivity(), "密码错误");
                        }


                    }
                }).setNeutralButton("取消", null).show();

            }
        });

    }

    public void setValue(int adress, String value, byte[] b_value) {
        try {
            TextView textView = map.get(adress);

            if (40030 == adress||40031 == adress) {
                textView.setText(TextTools.byteToHexString(b_value));
            }else if (40027 == adress) {
                int i1 = TextTools.byteToInt1(b_value[0]);
                int i2 = TextTools.byteToInt1(b_value[1]);
                textView.setText(i1+"."+i2);
            } else if (adress == 00001 || adress == 00002 || adress == 00010) {
                textView.setText("● " + value);
                if (!"ON".equals(value)) {
                        textView.setTextColor(Color.parseColor("#4FE919"));

                } else {
                    textView.setTextColor(Color.RED);
                }
            } else{
                textView.setText(value);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
